package gnippets.domain

import refcode.RefCode

/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
/**
 *  <p>Integration Tests for the gniplet</p>
 *
 *  @author <a href='mailto:kouphax@gmail.com'>James Hughes</a>
 */
 class GnipletTests extends GroovyTestCase {

    //=========================================================================
    //
    //                    U N I T   T E S T   S E T   U P
    //
    //=========================================================================
    void setUp(){
        
        /* create some ref data */
        def refCodeLangGroovy = new RefCode(
            domain:'language',
            value:'Groovy',
            meaning:'Groovy',
            sortOrder:0
        )
        
        def refCodeLangSQL = new RefCode(
            domain:'language',
            value:'SQL',
            meaning:'SQL',
            sortOrder:1
        )        
        
        /* save the ref data */
        if(!(refCodeLangSQL.save() && refCodeLangGroovy.save())){
            throw new Exception("Error in setup method. Save RefData failed.");
        }        
    }
    
    //=========================================================================
    //
    //         D E S C R I P T I O N   C O N S T R A I N T S   T E S T S
    //
    //=========================================================================
    
    void testContraintsDescriptionNull(){
    
        def testGniplet = new Gniplet(
            //description:'Test Description',
            code:'Test Code',
            language:'SQL'
        )
        
        assert testGniplet.validate() == false
        
        testGniplet.description = 'Test Description'
        
        assert testGniplet.validate() == true 
    }
    
    void testContraintsDescriptionBlank(){
    
        def testGniplet = new Gniplet(
            description:'',
            code:'Test Code',
            language:'SQL'
        )
        
        assert testGniplet.validate() == false
        
        testGniplet.description = 'Test Description'
        
        assert testGniplet.validate() == true 
    }    
    
    //=========================================================================
    //
    //                  C O D E   C O N S T R A I N T S   T E S T S
    //
    //=========================================================================
    
    void testContraintsCodeNull(){
    
        def testGniplet = new Gniplet(
            description:'Test Description',
            //code:'Test Code',
            language:'SQL'
        )
        
        assert testGniplet.validate() == false
        
        testGniplet.code = 'Test Code'
        
        assert testGniplet.validate() == true 
    }
    
    void testContraintsCodeBlank(){
    
        def testGniplet = new Gniplet(
            description:'Test Description',
            code:'',
            language:'SQL'
        )
        
        assert testGniplet.validate() == false
        
        testGniplet.code = 'Test Code'
        
        assert testGniplet.validate() == true 
    }      
    
    //=========================================================================
    //
    //             L A N G U A G E   C O N S T R A I N T S   T E S T S
    //
    //=========================================================================  
    
    void testContraintsLanguageNull(){
    
        def testGniplet = new Gniplet(
            description:'Test Description',
            code:'Test Code'//,
            //language:'SQL'
        )
        
        assert testGniplet.validate() == false
        
        testGniplet.language = 'SQL'
        
        assert testGniplet.validate() == true 
    }    
    
    void testContraintsLanguageBlank(){
    
        def testGniplet = new Gniplet(
            description:'Test Description',
            code:'Test Code',
            language:''
        )
        
        assert testGniplet.validate() == false
        
        testGniplet.language = 'SQL'
        
        assert testGniplet.validate() == true 
    }    
    
    void testContraintsLanguageInvalid(){
    
        def testGniplet = new Gniplet(
            description:'Test Description',
            code:'Test Code',
            language:'INVALID'
        )
        
        assert testGniplet.validate() == false
        
        testGniplet.language = 'SQL'
        
        assert testGniplet.validate() == true 
    }
}
